#pragma once
//#include "../game/Figure.h"
#include "../game/WoWLoader.h"
#include "../game/Resource.h"
#include "../math/coordinate.h"
#include "../stdafx.h"
	class Coordinate;
	extern LPDIRECT3DDEVICE9 g_pd3dDevice;
	struct Cell
	{
		Cell()
		{
			fid=-1;
		}

		D3DXVECTOR3 midPos;
		D3DXVECTOR3 minPos;
		D3DXVECTOR3 maxPos;
		int32 fid;
		bool move;
	};

	class Resource;
	class Figure;
	class WoWLoader;
	class World : public Component
	{
	public:
		World(void);
		~World(void);

		void addedToObject();

		WorldPoint m_curCell;
		WorldPoint m_moveStart;
		WorldPoint m_moveEnd;
		D3DXVECTOR3 m_moveInc;
		int32 m_wWidth;
		int32 m_wHeight;
		float m_sWidth;
		float m_sHeight;

		bool LoadGround(int32,int32,const char*);
		HRESULT CreateGround();
		HRESULT CreateMoveCell();
		void InitWorld(Message const & msg);
		void AddFigureInCell(Message const & msg);
		Cell m_cell[32][32];
		int32 GetCurCellFid();
		D3DXVECTOR3 GetCellPos(int32,int32);
		void GetCurCellPos(Message const & msg);
		void RenderMoveCell(Message const & msg);
		void UpdateCursorCell(Message const & msg);
		void SetFigureMove(WorldPoint);
		void DrawGround();
		void Draw(Message const & msg);
		void TransformCell(Message const & msg);
		void InitCell();
		void ClearMove(Message const & msg);
		void FigureMove(Message const & msg);
		void UpdateFigurePos(WorldPoint wp);
		bool m_figMove;
		bool CheckCell(int32,int32);
		bool GetCellIndex(D3DXVECTOR3 pos,int32* x,int32* y);
	private:
		bool m_move;
		int32 m_moveCount;
		LPDIRECT3DTEXTURE9 m_tex;
		LPDIRECT3DTEXTURE9 m_curTex;
		LPDIRECT3DINDEXBUFFER9 m_id;
		LPDIRECT3DVERTEXBUFFER9 m_groundVB;
		LPDIRECT3DVERTEXBUFFER9 m_moveVB;
		LPDIRECT3DVERTEXBUFFER9 m_curVb;

		inline void World::CreateQuad(D3DXVECTOR3* pos,CUSTOMVERTEX* pVertices,int32 c)
		{
			pVertices[c*6].position=*pos;
			pVertices[c*6].tu=0.0f;
			pVertices[c*6].tv=0.0f;

			pVertices[c*6+1].position=D3DXVECTOR3(pos->x+WIDTH_PRE_CELL,pos->y,pos->z);
			pVertices[c*6+1].tu=1.0f;
			pVertices[c*6+1].tv=0.0f;

			pVertices[c*6+2].position=D3DXVECTOR3(pos->x+WIDTH_PRE_CELL,pos->y,pos->z+WIDTH_PRE_CELL);
			pVertices[c*6+2].tu=1.0f;
			pVertices[c*6+2].tv=1.0f;

			pVertices[c*6+3].position=*pos;
			pVertices[c*6+3].tu=0.0f;
			pVertices[c*6+3].tv=0.0f;

			pVertices[c*6+4].position=D3DXVECTOR3(pos->x+WIDTH_PRE_CELL,pos->y,pos->z+WIDTH_PRE_CELL);
			pVertices[c*6+4].tu=1.0f;
			pVertices[c*6+4].tv=1.0f;

			pVertices[c*6+5].position=D3DXVECTOR3(pos->x,pos->y,pos->z+WIDTH_PRE_CELL);
			pVertices[c*6+5].tu=0.0f;
			pVertices[c*6+5].tv=1.0f;
		}

	};